﻿package mortal.game.view.godLuckGift
{
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.activeTime.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.godLuckGift.data.*;
    import mortal.game.view.godLuckGift.item.*;
    import mortal.game.view.godLuckGift.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GodLuckGiftController extends Controller
    {
        private var _godMod:GodLuckGiftModule;
        private var _frameT:SecTimer;
        private var _active:SClientActive;
        private var _state:int;
        private var _countNum:int = 0;

        public function GodLuckGiftController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._godMod == null)
            {
                this._godMod = new GodLuckGiftModule();
                this._godMod.addEventListener(WindowEvent.SHOW, this.onShowWin);
                this._godMod.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._godMod;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.ActiveNotic, this.activesNoticHandler);
            Dispatcher.addEventListener(EventName.ActiveStart, this.onActionStar);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.onActionEnd);
            EventUtil.addSelfEvent(EventName.GodLuckGiftOpenModule, this.onOpenModule, true);
            LevelCheckUtil.addLevelUp(this.levelUp);
            return;
        }// end function

        private function onShowWin(event:Event) : void
        {
            this.addEs(true);
            return;
        }// end function

        private function onClose(event:Event) : void
        {
            this.addEs(false);
            return;
        }// end function

        private function addEs(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.GodLuckGiftGoAndCollect, this.onGoAndCollect, param1);
            EventUtil.addSelfEvent(EventName.GodLuckGiftFlyAndCollect, this.onFlyAndCollect, param1);
            return;
        }// end function

        private function addCollectEvs(param1:Boolean) : void
        {
            EventUtil.addNetEvent(ServerCommand.GodLuckGiftCollectUpdate, this.onCollectUpdate, param1);
            EventUtil.addNetEvent(ServerCommand.GodLuckGiftCollectUpdate, this.onCollectUpdate, param1);
            EventUtil.addNetEvent(ServerCommand.GodLuckGiftCollectUpdate, this.onCollectUpdate, param1);
            EventUtil.addNetEvent(ServerCommand.BackPackItemsAdd, this.onItemsCollect, param1);
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type != EActiveType._EActiveTypeMoneyCatcher)
            {
                return;
            }
            if (ActiveTimeUtil.isInDoubleTimeByType(_loc_2.type))
            {
                _loc_3 = _loc_2.endDt.time - ClockManager.instance.nowDate.time;
                if (_loc_3 <= 0)
                {
                    return;
                }
                this._state = 2;
            }
            else
            {
                _loc_3 = _loc_2.startDt.time - ClockManager.instance.nowDate.time;
                if (_loc_3 <= 0)
                {
                    return;
                }
                this._state = 1;
            }
            if (LevelCheckUtil.isLevelEnougth(58))
            {
                GodLuckGiftIcon.instance.show();
                GodLuckGiftIcon.instance.updateOpenDt(_loc_2.startDt, this._state);
                GodLuckGiftIcon.instance.playEffect();
            }
            else if (GodLuckGiftIcon.hasInstance())
            {
                GodLuckGiftIcon.instance.hide();
            }
            return;
        }// end function

        private function onActionStar(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeMoneyCatcher)
            {
                this._active = _loc_2;
                this.addCollectEvs(true);
                GodLuckGiftUtil.clear();
                this._state = 2;
                if (LevelCheckUtil.isLevelEnougth(58))
                {
                    GodLuckGiftIcon.instance.show();
                    GodLuckGiftIcon.instance.updateOpenDt(this._active.endDt, 2);
                    GodLuckGiftIcon.instance.playEffect();
                }
                else
                {
                    GodLuckGiftIcon.instance.hide();
                }
            }
            return;
        }// end function

        private function onActionEnd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeMoneyCatcher)
            {
                this._active = null;
                this.addCollectEvs(false);
                GodLuckGiftIcon.instance.hide();
                this.showPassWin();
            }
            return;
        }// end function

        private function initTimer() : void
        {
            if (this._frameT == null)
            {
                this._frameT = new SecTimer(1);
                this._frameT.addListener(TimerType.ENTERFRAME, this.onTime);
                this._frameT.start();
            }
            return;
        }// end function

        private function onTime(param1) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this._countNum + 1;
            _loc_2._countNum = _loc_3;
            if (this._countNum < 30)
            {
                return;
            }
            this._countNum = 0;
            if (GodLuckGiftCollectPanel2.hasInstance() && GodLuckGiftCollectPanel2.instance.isHide == false)
            {
                GodLuckGiftCollectPanel2.instance.hide();
            }
            if (this._frameT)
            {
                this._frameT.stop();
                if (this._active == null)
                {
                    this._frameT.dispose();
                    this._frameT = null;
                }
            }
            return;
        }// end function

        private function stopTimer() : void
        {
            if (this._frameT)
            {
                this._frameT.stop();
                this._frameT.dispose();
                this._frameT = null;
            }
            return;
        }// end function

        private function initHd(param1:Object) : void
        {
            var _loc_2:* = GodLuckGiftUtil.getTestItems();
            GodLuckPassWin.instance.show();
            GodLuckPassWin.instance.updateInfo(1, 2, _loc_2);
            return;
        }// end function

        private function levelUp(param1:Object) : void
        {
            if (this._active && this._active.type == EActiveType._EActiveTypeMoneyCatcher)
            {
                if (LevelCheckUtil.isLevelEnougth(58))
                {
                    GodLuckGiftIcon.instance.show();
                    GodLuckGiftIcon.instance.updateOpenDt(this._active.endDt, this._state);
                }
                else if (GodLuckGiftIcon.hasInstance() && GodLuckGiftIcon.instance.isHide == false)
                {
                    GodLuckGiftIcon.instance.hide();
                }
            }
            return;
        }// end function

        private function onOpenModule(event:DataEvent) : void
        {
            if (view.isHide)
            {
                view.show();
            }
            return;
        }// end function

        private function onGoAndCollect(event:DataEvent) : void
        {
            GodLuckGiftUtil.gotoCollect();
            return;
        }// end function

        private function onFlyAndCollect(event:DataEvent) : void
        {
            GodLuckGiftUtil.onFlyHd();
            if (_view && _view.isHide == false)
            {
                _view.hide();
            }
            return;
        }// end function

        private function onItemsCollect(param1:Object) : void
        {
            var _loc_3:ItemData = null;
            var _loc_4:Object = null;
            var _loc_2:* = param1 as Array;
            if (_loc_2 == null || _loc_2.length < 3)
            {
                return;
            }
            if (UpdateCode.EUpdateCodeBossDropDirToBagDir == _loc_2[1])
            {
                _loc_3 = _loc_2[0] as ItemData;
                _loc_4 = {type:"item", value:[_loc_3, _loc_2[2]]};
                this.onCollectUpdate(_loc_4);
            }
            return;
        }// end function

        private function onCollectUpdate(param1:Object) : void
        {
            var _loc_2:* = ConstConfig.instance.getValueByName("TMoneyCatcherMapId");
            if (Game.mapInfo == null || Game.mapInfo.mapId != _loc_2)
            {
                return;
            }
            var _loc_3:* = GodLuckGiftUtil.items;
            switch(param1.type)
            {
                case "coin":
                {
                    GodLuckGiftUtil.coin = GodLuckGiftUtil.coin + param1.value;
                    break;
                }
                case "gold":
                {
                    GodLuckGiftUtil.gold = GodLuckGiftUtil.gold + param1.value;
                    break;
                }
                case "item":
                {
                    if (GodLuckGiftUtil.CollectMapId != Game.mapInfo.mapId)
                    {
                        return;
                    }
                    if (_loc_3 == null)
                    {
                        _loc_3 = [];
                    }
                    if (param1.value)
                    {
                        _loc_3 = this.converArr(param1.value);
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            this._countNum = 0;
            this.initTimer();
            this._frameT.start();
            if (GodLuckGiftCollectPanel2.instance.isHide)
            {
                GodLuckGiftCollectPanel2.instance.show();
                GodLuckGiftCollectPanel2.instance.updateOpenDt(this._active.endDt);
            }
            var _loc_4:* = GodLuckGiftUtil.coin;
            var _loc_5:* = GodLuckGiftUtil.gold;
            GodLuckGiftUtil.items = _loc_3;
            GodLuckGiftCollectPanel2.instance.updateInfo(_loc_4, _loc_5, _loc_3);
            return;
        }// end function

        private function showPassWin() : void
        {
            if (Game.mapInfo && Game.mapInfo.mapId != GodLuckGiftUtil.CollectMapId)
            {
                return;
            }
            var _loc_1:* = GodLuckGiftUtil.coin;
            var _loc_2:* = GodLuckGiftUtil.gold;
            var _loc_3:* = GodLuckGiftUtil.items;
            GodLuckPassWin.instance.show();
            GodLuckPassWin.instance.updateInfo(_loc_1, _loc_2, _loc_3);
            return;
        }// end function

        private function converArr(param1:Array) : Array
        {
            var _loc_7:int = 0;
            var _loc_8:ItemData = null;
            var _loc_9:ItemData = null;
            if (param1 == null || param1.length < 2)
            {
                return GodLuckGiftUtil.items;
            }
            var _loc_2:Array = [];
            var _loc_3:* = GodLuckGiftUtil.items;
            var _loc_4:* = GodLuckGiftUtil.itemObj;
            if (_loc_3 == null)
            {
                var _loc_10:* = [];
                GodLuckGiftUtil.items = [];
                _loc_3 = _loc_10;
            }
            if (_loc_4 == null)
            {
                var _loc_10:* = {};
                GodLuckGiftUtil.itemObj = {};
                _loc_4 = _loc_10;
            }
            var _loc_5:* = param1[0] as ItemData;
            if (_loc_5 == null)
            {
                return GodLuckGiftUtil.items;
            }
            var _loc_6:* = param1[1];
            if (_loc_4[_loc_5.itemCode] != null)
            {
                _loc_7 = int(_loc_4[_loc_5.itemCode]);
                _loc_8 = _loc_3[_loc_7];
                if (_loc_8 == null)
                {
                    return GodLuckGiftUtil.items;
                }
                _loc_8.itemAmount = _loc_8.itemAmount + _loc_6;
            }
            else
            {
                _loc_4[_loc_5.itemCode] = _loc_3.length;
                _loc_9 = new ItemData(_loc_5.itemCode);
                _loc_9.itemAmount = _loc_6;
                _loc_3.push(_loc_9);
            }
            return _loc_3;
        }// end function

        public function get active() : SClientActive
        {
            return this._active;
        }// end function

    }
}
